package com.samsung.svmc.moneymanager.utility;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import com.samsung.svmc.moneymanager.object.TheLoaiTaiKhoan;

import android.content.Context;
import android.util.Log;

public class TheLoaiTaiKhoanUtility {
	
	protected Context contect;
	protected String tagException = "Exception";
	public TheLoaiTaiKhoanUtility(Context c){
		contect = c;
	}
	
	public String ReadTheLoaiJson(){
		String json = null;		
		try{
			InputStream in = contect.getAssets().open("theloaitaikhoan.json");
			int size = in.available();
			byte[] buffer = new byte[size];
			in.read(buffer);
			in.close();
			json = new String(buffer, "UTF-8");
		}catch(IOException ex){
			Log.d(tagException, "khong the doc file json the loai tai khoan");
		}		
		return json;
	}
	/******************* XML ************************************************/
	public List<TheLoaiTaiKhoan> XmlUtilyti(){
		List<TheLoaiTaiKhoan> lv = new ArrayList<TheLoaiTaiKhoan>();
		
		XmlPullParserFactory pullFactory;
		try{
			pullFactory = XmlPullParserFactory.newInstance();
			XmlPullParser pullParser = pullFactory.newPullParser();
			InputStream in = contect.getApplicationContext().getAssets().open("theloaitaikhoan.xml");
			pullParser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
			pullParser.setInput(in, null);
			lv = parseXML(pullParser);
		}catch(XmlPullParserException ex){
			Log.d(tagException, "Xml parser error: "+ex.getMessage());
		}catch (IOException e) {
			Log.d(tagException, "IO error: "+e.getMessage());
		}
		
		return lv;
	}
	 
	protected List<TheLoaiTaiKhoan> parseXML(XmlPullParser pull) throws XmlPullParserException, IOException{
		List<TheLoaiTaiKhoan> lv = new ArrayList<TheLoaiTaiKhoan>();
		int evenType = pull.getEventType();
		TheLoaiTaiKhoan tk = null;
		while(evenType != XmlPullParser.END_DOCUMENT){
			String name = "";
			switch (evenType) {
			case XmlPullParser.START_DOCUMENT:
				
				break;
			case XmlPullParser.START_TAG:
				name = pull.getName();
				if(name.equalsIgnoreCase("theloai")){
					// bat dau mot node
					tk = new TheLoaiTaiKhoan();
				}else if(tk != null){
					// khai thac element cua node
					if(name.equalsIgnoreCase("id")){
						tk.setId(Integer.parseInt(pull.nextText()));
					} else if(name.equalsIgnoreCase("name")){
						tk.setTenTheLoai(pull.nextText());
					}
				}
				break;
			case XmlPullParser.END_TAG:
				name = pull.getName();
				if(name.equalsIgnoreCase("theloai") && tk != null){
					// add node
					lv.add(tk);
				}
				break;

			default:
				break;
			}
			evenType = pull.next();
		}
		return lv;
	}
	/************************************************************************/

}
